package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.VideoLiveAdapter;
import com.xgkj.diyiketang.fragment.VideoLiveLFragment;
import com.xgkj.diyiketang.fragment.VideoLiveVFragment;

import java.util.ArrayList;

public class VideoLiveActivity extends BaseActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {
    private VideoLiveVFragment mVideoLiveVFragment;
    private VideoLiveLFragment mVideoLiveLFragment;
    private ViewPager myviewpager;
    //fragment的集合，对应每个子页面
    private ArrayList<Fragment> fragments;
    //选项卡中的按钮
    private Button btn_video;
    private Button btn_live;
    //作为指示标签的按钮
    private ImageView cursor;
    //标志指示标签的横坐标
    float cursorX = 0;
    //所有按钮的宽度的集合
    private int[] widthArgs ;
    //所有按钮的集合
    private Button[] btnArgs;
    private RelativeLayout back;
    private LinearLayout bottomlinear,videolist_cursorarea;
    private TextView beautiful_more;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_live);
        initView();
    }

    public void initView() {
        Intent intent = getIntent();
        String title = intent.getStringExtra("TITLE");
        TextView titleText = (TextView) findViewById(R.id.videolist_actionbar_text);
        if (!TextUtils.isEmpty(title)) {
            titleText.setText(title);
        }
        myviewpager = (ViewPager) this.findViewById(R.id.videolist_viewpager);
        beautiful_more = (TextView) findViewById(R.id.beautiful_more);
        bottomlinear = (LinearLayout) findViewById(R.id.bottomlinear);
        videolist_cursorarea = (LinearLayout) findViewById(R.id.videolist_cursorarea);
        btn_video = (Button) this.findViewById(R.id.videolive_btn_video);
        btn_live = (Button) this.findViewById(R.id.videolive_btn_live);
        back = (RelativeLayout) this.findViewById(R.id.videolist_back);
        btnArgs = new Button[]{btn_video, btn_live};

        cursor = (ImageView) this.findViewById(R.id.videolist_imageView);
        cursor.setBackgroundColor(getResources().getColor(R.color.shouye_tab_color_selected));
        //通过此方法设置指示器的初始大小和位置
        btn_video.post(new Runnable() {
            @Override
            public void run() {
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) cursor.getLayoutParams();
                //减去边距*2，以对齐标题栏文字
                lp.width = btn_video.getWidth() - dip2px(VideoLiveActivity.this, 100);
                cursor.setLayoutParams(lp);
                cursor.setX(-(btn_video.getPaddingLeft() - dip2px(VideoLiveActivity.this, 50)));
            }
        });
        widthArgs = new int[]{btn_video.getWidth(), btn_live.getWidth()};
        myviewpager.setOnPageChangeListener(this);
        btn_video.setOnClickListener(this);
        btn_live.setOnClickListener(this);
        back.setOnClickListener(this);
        beautiful_more.setOnClickListener(this);

        mVideoLiveVFragment = new VideoLiveVFragment();
        mVideoLiveLFragment = new VideoLiveLFragment();

        fragments = new ArrayList<Fragment>();
        if (title.equals("最美教师")){
            fragments.add(mVideoLiveLFragment);
        }else {
            fragments.add(mVideoLiveVFragment);
            fragments.add(mVideoLiveLFragment);
        }


        VideoLiveAdapter adapter = new VideoLiveAdapter(getSupportFragmentManager(), fragments);
        myviewpager.setAdapter(adapter);

        resetButtonColor();
        btn_video.setTextColor(getResources().getColor(R.color.green_light));

        //最美老师需要将这两个button隐藏掉。2017.4.26，李睿
        if (title.equals("最美教师")){
            bottomlinear.setVisibility(View.GONE);
            videolist_cursorarea.setVisibility(View.GONE);
            beautiful_more.setVisibility(View.VISIBLE);
        }

    }

    //重置所有按钮的颜色
    public void resetButtonColor() {
        btn_video.setTextColor(getResources().getColor(R.color.gray));
        btn_live.setTextColor(getResources().getColor(R.color.gray));
    }

    @Override
    public void onClick(View whichbtn) {
        // TODO Auto-generated method stub

        switch (whichbtn.getId()) {
            case R.id.videolive_btn_video:
                myviewpager.setCurrentItem(0);
                cursorAnim(0);
                break;
            case R.id.videolive_btn_live:
                myviewpager.setCurrentItem(1);
                cursorAnim(1);
                break;
            case R.id.videolist_back:
                finish();
                break;
            case R.id.beautiful_more:
                if (getEatShowApp().getmUserInfo() == null){
                    startActivity(new Intent(VideoLiveActivity.this,LoginActivity.class));
                }else {
                    startActivity(new Intent(VideoLiveActivity.this,RankingActivity.class));
                }

        }
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageSelected(int arg0) {
        // TODO Auto-generated method stub

        //每次滑动首先重置所有按钮的颜色
        resetButtonColor();
        //将滑动到的当前按钮颜色设置为绿色
        btnArgs[arg0].setTextColor(getResources().getColor(R.color.green_light));
        cursorAnim(arg0);
    }

    //指示器的跳转，传入当前所处的页面的下标
    public void cursorAnim(int curItem) {
        if (curItem == 0){
            btn_video.post(new Runnable() {
                @Override
                public void run() {
                    LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) cursor.getLayoutParams();
                    //减去边距*2，以对齐标题栏文字
                    lp.width = btn_video.getWidth() - dip2px(VideoLiveActivity.this, 100);
                    cursor.setLayoutParams(lp);
                    cursor.setX(-(btn_video.getPaddingLeft() - dip2px(VideoLiveActivity.this, 50)));
                }
            });
        }else if (curItem == 1){
            btn_live.post(new Runnable() {
                @Override
                public void run() {
                    LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) cursor.getLayoutParams();
                    //减去边距*2，以对齐标题栏文字
                    lp.width = btn_video.getWidth() - dip2px(VideoLiveActivity.this, 100);
                    cursor.setLayoutParams(lp);
                    cursor.setX(-(btn_video.getPaddingLeft() - dip2px(VideoLiveActivity.this, 50))+btn_video.getWidth());
                }
            });
        }

    }


    /**
     * dp转px
     */
    private int dip2px(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

}
